package com.outfood.utils;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.List;
import java.util.Random;

/**
 * @Author huisonglin
 * @Description
 * @PackageName outfoodcat-master
 * @Package com.outfood.utils
 * @Date 2024-07-11 9:59
 * @Version 1.0
 */
public class BingTopUtil {

//    public static void main(String[] args) {
//        // 读取图片base64编码数据
//        String captchaData = BingTopUtil.getImageStr("图片路径.jpg");
//        // post图片数据，账号信息，进行打码
//        String returnStr = BingTopUtil.sendPost(
//                "http://www.bingtop.com/ocr/upload/",
//                "username=用户名&password=密码&captchaType=验证码类型&captchaData="+captchaData,"UTF-8"
//        );
//        System.out.println(returnStr);
//    }

    public static List<Integer> splitIntoRandomNumbers(int total, int maxParts) {
        List<Integer> parts = new ArrayList<>();
        Random random = new Random();

        // 确保total是非负的
        if (total < 0) {
            throw new IllegalArgumentException("Total cannot be negative");
        }

        // 生成随机数直到达到最大数量或和为0
        while (total > 0 && parts.size() < maxParts) {
            int part = random.nextInt(total + 1); // 生成一个[0, total]范围内的随机数
            if(part < 100  ){
                total -= part; // 更新剩余总和
                parts.add(part); // 添加到部分列表
            }

        }

        // 如果还有剩余部分，则将剩余的总和分配为最后一个随机数
        if (total > 0 ) {
            parts.set(parts.size() - 1, parts.get(parts.size() - 1) + total);
        }

        return parts;
    }

    public static void main(String[] args) {
        int total = 280; // 假设我们要拆分的总和是100
        int maxParts = 6; // 最多可以产生10个数字
        List<Integer> result = splitIntoRandomNumbers(total, maxParts);

        // 打印结果
        for (int part : result) {
            System.out.println(part);
        }
    }

    public static String getImageStr(String imgFile) {
        /**
         * @Description: 根据图片地址转换为base64编码字符串
         * @return String
         */
        FileInputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Encoder encoder = Base64.getEncoder();
        String encodedata = encoder.encodeToString(data);
        String encode="";
        try {
            encode = URLEncoder.encode(encodedata,"UTF-8");
        } catch (Exception e) {
            System.out.println("转码异常!"+e);
            e.printStackTrace();
        }
        return encode;
    }

    public static String sendPost(String url, String param,String charset) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        String line;
        StringBuffer sb=new StringBuffer();
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性 设置请求格式
            conn.setRequestProperty("contentType", charset);
            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            //设置超时时间
            conn.setConnectTimeout(60*1000);
            conn.setReadTimeout(60*1000);

            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8"));
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应    设置接收格式
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(),charset));
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            result=sb.toString();
        } catch (Exception e) {
            System.out.println("发送 POST请求出现异常!"+e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }


}
